package org.sklse.lab.controller;

import org.sklse.lab.bean.User;
import org.sklse.lab.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value="register",method=RequestMethod.GET)
    public String signup(User user,HttpSession httpSession) throws Exception{
        if(userService.UserSignup(user)){
            System.out.println("注册成功");
            return "注册成功";

        }else{
            return "用户已存在";
        }
    }

    @RequestMapping(value="login",method=RequestMethod.GET)
    public String login(User user,HttpSession httpSession) throws Exception{
        User user1=userService.findByPhonenumber(user.getPhonenumber());
        if(user1!=null){
            if(user1.getPassword().equals(user.getPassword())){
                 httpSession.setAttribute("phonenumber",user.getPhonenumber());
                  return "登录成功";
            }else{
                return "用户名或密码错误";
            }

        }else{
            return "用户不存在";
        }

    }
}
